<?php
	session_start();
	//Validate if the user has a valid username and password
	if(!array_key_exists("username", $_SESSION)){
		header("Location: ../index.php"); //The user didn't have a valid session, return to the index page
	}
	
	//User has credentials to validate
	$conn = mysql_connect("", "piedu_user", "q&&^%@8ezO8+");
	mysql_select_db("piedu_db");
	
	//Get all the user friends
	$sql = "SELECT * from community_contacts where username = '{$_SESSION['username']}'";
	$query = mysql_query($sql, $conn);
	if(mysql_num_rows($query) != 0){
		while($row = mysql_fetch_assoc($query)){
			$users = "'".$row['contact']."',";
		}
	} else {
		$users = $_SESSION['username'];
	}
	$users = rtrim($users, ",");
	
	//Select all the last messages from the user contact list
	if(mysql_num_rows($query) != 0){
		$sql = "SELECT * FROM community WHERE username IN ($users) ORDER BY timestamp DESC LIMIT 20";
	} else {
		$sql = "SELECT * FROM community WHERE username = '{$_SESSION['username']}' ORDER BY timestamp DESC LIMIT 20";
	}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Pie Education | Community</title>
<Meta name="Keywords" content="education, course, learning, online, virtual, e-learning, school, student, teacher, homework, educational platform, educational system, free tool">
<Meta name="Description" content="Pie Education is a internet based Course Management System (CMS)">
<Meta name="Author" content="Rob Keller">
<Meta name="Publisher" content="Aaron Aceves">
<Meta name="Copyright" content="Copyright© 2012, Pie Education">
<Meta name="Revisit-After" content="30 days">
<Meta HTTP-EQUIV="Expires" content="none">
<Meta name="Pagetopic" content="Education">
<Meta name="Audience" content=" All">
<Meta name="Robots" content="INDEX,FOLLOW">
<Meta name="Content-Language" content="English">
<link rel="shortcut icon" type="image/x-icon" href="/favicon.ico" />
<link rel="stylesheet" href="../css/style.css" />
</head>

<body>
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td height="40" bgcolor="#FF8C00" align="center"><table width="1100" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td width="250"><table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
          <tr>
            <td width="150"><img src="../images/small-logo.png" width="150" height="30" alt="PIE.EDU" /></td>
            <td width="50" align="center"><a href="#"><img src="../images/add.png" alt="Add User" width="20" height="19" border="0" /></a></td>
            <td width="50" align="center"><a href="#"><img src="../images/new_msn.png" alt="New Message" width="20" height="12" border="0" /></a></td>
            <td width="50" align="center"><a href="#"><img src="../images/new_notification.png" alt="New Notification" width="20" height="20" border="0" /></a></td>
          </tr>
        </table></td>
        <td width="700" align="center"><form id="form1" name="form1" method="post" action="search.php">
          <label for="search"></label>
          <input name="search" type="text" class="community_search" id="search" />
        </form></td>
        <td width="150">&nbsp;</td>
      </tr>
    </table></td>
  </tr>
  <tr>
    <td height="600" valign="top" bgcolor="#FFFFFF"><table width="1100" border="0" align="center" cellpadding="0" cellspacing="0">
      <tr>
        <td>&nbsp;</td>
        <td height="30" align="center" style="padding-top:10px;"><form id="form2" name="form2" method="post" action="community_update.php">
          <label for="status_update"></label>
          <input name="status_update" type="text" class="community_update" id="status_update" value="What's on you mind?" />
          <input name="status_to" type="hidden" id="status_to" value="<?= $_SESSION['username'] ?>" />
        </form></td>
        <td>&nbsp;</td>
      </tr>
      <tr>
        <td width="250" valign="top"><table width="80%" border="0" align="left" cellpadding="2" cellspacing="1">
          
          <tr>
            <td width="25" align="center"><img src="../images/pointer.png" width="24" height="24" alt="News Feed" /></td>
            <td><a href="index.php"><strong>News Feed</strong></a></td>
            </tr>
          <tr>
            <td align="center"><img src="../images/new_msn.png" width="24" height="16" alt="New Message" /></td>
            <td><a href="#"><strong>Messages</strong></a></td>
            </tr>
          <tr>
            <td align="center"><img src="../images/add.png" width="23" height="22" alt="Friends" /></td>
            <td><a href="main.php?username=<?= $_SESSION['username'] ?>"><strong>Profile</strong></a></td>
          </tr>
          <tr>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
          </tr>
          <tr>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
          </tr>
          <tr>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
          </tr>
        </table></td>
        <td width="700">
        <!-- News feed scroll -->
        <?php
		$query = mysql_query($sql, $conn);
		while($data = mysql_fetch_assoc($query)){
			$sql = "SELECT * FROM community_reply WHERE id = {$data['id']} LIMIT 2";
			$reply_data = mysql_query($sql, $conn);
			$date = date("F l Y at G:i:s", $data['timestamp']);
        echo '<table width="95%" border="0" align="center" cellpadding="0" cellspacing="0" style="border-bottom:thin; border-bottom-color:#CCC; border-bottom-style:solid; padding-top:10px">
          <tr>
            <td width="60" height="50" rowspan="2" align="center" valign="top" style="padding-top:5px"><img src="../images/profile/'.$_SESSION['username'].'.jpg" width="48" height="48" alt="pic" /></td>
            <td style="padding-top:10px"><strong><a href="main.php?username='.$data['username'].'">'.$data['username'].'</a></strong> wrote on <strong><a href="main.php?username='.$data['to'].'">'.$data['to'].'</a></strong></td>
          </tr>
          <tr>
            <td align="justify"><p>'.$data['message'].'</p></td>
          </tr>
          <tr>
            <td>&nbsp;</td>
            <td style="padding-bottom:5px"><table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
              <tr>
                <td height="20" style="color:#999"><a href="#">Like</a> - Posted on '.$date.'</td>
                </tr>
              <tr>
                <td><div id="message">';
				if(mysql_num_rows($reply_data)){
					while($reply_message = mysql_fetch_assoc($reply_data)){
						echo '<p><strong><a href="main.php?username='.$reply_message['username'].'">'.$reply_message['username'].'</a></strong></p>
						<p>'.$reply_message['message'].'</p>';
					}
				}
				echo'</div></td>
                </tr>
              <tr>
                <td style="padding-bottom:5px"><form action="community_new_message.php" method="post" id="'.$data['id'].'">
                  <label for="message"></label>
                  <input name="message" type="text" class="community_message" id="message" />
				  <input name="message_id" type="hidden" id="message_id" value="'.$data['id'].'" />
				  </form>
                  </td>
                </tr>
              </table></td>
          </tr>
        </table>';
		}
        ?>
        <!-- End the news feed scroll -->
        </td>
        <td width="150">&nbsp;</td>
      </tr>
    </table></td>
  </tr>
  <tr>
    <td height="40" bgcolor="#FF9933" align="center"><a href="../index.php" class="bottom-general-menu" style="color:#FFF">Home</a> <a href="../platform.php" class="bottom-general-menu" style="color:#FFF">Platform</a> <a href="../piephone.php" class="bottom-general-menu" style="color:#FFF">Pie Phone</a> <a href="../company_training.php" class="bottom-general-menu" style="color:#FFF">Company Training</a> <a href="../jobs.php" class="bottom-general-menu" style="color:#FFF">Jobs</a> <a href="../press.php" class="bottom-general-menu" style="color:#FFF">Press</a> <a href="../community/" class="bottom-general-menu" style="color:#FFF">Community</a> <a href="../mobile.php" class="bottom-general-menu" style="color:#FFF">Mobile</a> <a href="support/" class="bottom-general-menu" style="color:#FFF">Help</a> <a href="../blog/" class="bottom-general-menu" style="color:#FFF">Blogs</a> <a href="../forums/" class="bottom-general-menu" style="color:#FFF">Forums</a><a href="../about.php" class="bottom-general-menu" style="color:#FFF">About</a> <a href="../terms.html" class="bottom-general-menu" style="color:#FFF">Terms of Use</a> <a href="../privacy.html" class="bottom-general-menu" style="color:#FFF">Privacy Policy</a> <a href="../contact-us.php" class="bottom-general-menu" style="color:#FFF">Contact Us</a></td>
  </tr>
</table>
</body>
</html>
